package com.example.broadcasttutital;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.broadcasttutital.boradcast.BatteryReceiver;
import com.example.broadcasttutital.boradcast.NetworkChangeReceiver;

public class MainActivity extends AppCompatActivity {
    private IntentFilter networkFilter,batteryFilter;
    private NetworkChangeReceiver networkReceiver;
    private BatteryReceiver batteryReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void openNetworkBroadcast(View view) {
        networkFilter = new IntentFilter();
        networkFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkReceiver = new NetworkChangeReceiver();
        registerReceiver(networkReceiver,networkFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkReceiver);
        unregisterReceiver(batteryReceiver);
    }

    public void closeNetworkBroadcast(View view) {
        unregisterReceiver(networkReceiver);
    }

    public void openBatteryBroadcast(View view) {
        batteryFilter = new IntentFilter();
        batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        batteryReceiver = new BatteryReceiver();
        registerReceiver(batteryReceiver,batteryFilter);
    }

    public void closeBatteryReceiver(View view) {
        unregisterReceiver(batteryReceiver);
    }

    public void sendBroadcast(View view) {
        Intent intent = new Intent("com.example.broadcasttutital.boradcast.MY_BROADCAST");
        intent.setComponent(new ComponentName(getPackageName(),"com.example.broadcasttutital.boradcast.MyBroadcastReceiver"));
        intent.putExtra("name","JohnConnor");

        sendBroadcast(intent,"com.example.MY_BROADCAST_PERMISSION");
    }
}